define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        config = require("./setting");

    var async = function(u, c, t, g, d) {
        $.ajax(u, {
            type: g || "post",
            data: d,
            dataType: "json"
        }).done(function(d) {
            c && c(d);
        }).fail(function() {
            //config.msg.info(t || config.lang.exception)
        });
    }
    return {
        get: function(u, c, t, d) {
            async(u, c, t, 'get', d)
        },
        post: function(u, c, t, g, d) {
            async(u, c, t, g, d)
        },
        postd: function(u, c, d) {
            async(u, c, false, false, d)
        },
        transMsgTime: function(a) {
            var b, c, d, e, f, g = {
                0: "周日",
                1: "周一",
                2: "周二",
                3: "周三",
                4: "周四"
            };
            return a ? (c = new Date, b = "object" == typeof a && a.constructor === Date ? a : new Date(a), d = (c - b) / 1e3, 60 > d ? "刚刚" : 3600 > d ? Math.floor(d / 60) + "分钟前" : 10800 > d ? Math.floor(d / 3600) + "小时前" : (f = {
                year: b.getFullYear(),
                month: b.getMonth() + 1,
                date: b.getDate(),
                day: b.getDay(),
                hours: b.getHours() >= 10 ? b.getHours() : "0" + b.getHours().toString(),
                min: b.getMinutes() >= 10 ? b.getMinutes() : "0" + b.getMinutes().toString()
            }, e = {
                year: c.getFullYear(),
                month: c.getMonth() + 1,
                date: c.getDate(),
                day: c.getDay()
            }, f.date === e.date && f.month === e.month && f.year === e.year ? "今天" + f.hours + ":" + f.min : e.date - f.date === 1 && 172800 > d ? "昨天" + f.hours + ":" + f.min : e.day - f.day > 0 && 604800 > d ? g[f.day] + f.hours + ":" + f.min : f.year === e.year ? f.month + "月" + f.date + "日" : f.year + "年" + f.month + "月" + f.date + "日")) : ""
        },
        getFormatDateStr: function(date) {
            var a = new Date(1e3 * date),
                b = a.getFullYear(),
                c = (a.getDate(), a.getMonth() + 1);
            return b + "年" + c + "月"
        },
        getFormatDateStrNow: function() {
            return new Date().format("yyyy年M月");
        },
        getfileExt: function(filename) {
            return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename.toLowerCase()) : '';
        },
        eventarget: function(e) {
            var e = e || window.event;
            return e.target || e.srcElement;
        },
        validKindEditor: function() {
            var flag = true;
            if (typeof KindEditor !== "undefined" && KindEditor.instances) {
                $.each(KindEditor.instances, function() {
                    this.sync();
                    var $element = $(this.srcElement[0]);
                    var r = $element.data("ruleRequired"),
                        m = $element.data("msgRequired"),
                        e = $element.data("ruleRangelength"),
                        em = $element.data("msgRangelength");
                    var v = $.trim($element.val()).replace(/(&nbsp;)|\s|\u00a0/g, '');
                    if (r) {
                        if (v.length == 0) {
                            var msg = m;
                            config.msg.error(msg || "内容不能为空")
                            flag = false;
                        }
                    };
                    if (e) {
                        var min = e[0];
                        var max = e[1];
                        var vv = $element.val();
                        if (vv.length < min || vv.length > max) {
                            config.msg.error(em || "内容不能小于{0}且大于{1}".format(min, max))
                            flag = false;
                        }
                    }
                })
            }
            return flag;
        },
        OpenWindowWithPost: function(url, name, params, type) {
            var form = document.createElement("form");
            form.setAttribute("method", type);
            form.setAttribute("action", url);
            form.setAttribute("target", name);
            for (var i in params) {
                var input = document.createElement('input');
                input.type = 'hidden';
                input.name = params[i].name;
                input.value = params[i].value;
                form.appendChild(input);
            }
            document.body.appendChild(form);
            form.submit();
            document.body.removeChild(form);
        },
        URL: function(url) {
            var ourl = url || window.location.href;
            var href = ""; //?前面部分
            var params = {}; //url参数对象
            var jing = ""; //#及后面部分
            var init = function() {
                var str = ourl;
                var index = str.indexOf("#");
                if (index > 0) {
                    jing = str.substr(index);
                    str = str.substring(0, index);
                }
                index = str.indexOf("?");
                if (index > 0) {
                    href = str.substring(0, index);
                    str = str.substr(index + 1);
                    var parts = str.split("&");
                    for (var i = 0; i < parts.length; i++) {
                        var kv = parts[i].split("=");
                        params[kv[0]] = kv[1];
                    }
                } else {
                    href = ourl;
                    params = {};
                }
            };
            this.set = function(key, val) {
                params[key] = encodeURIComponent(val);
            };
            this.remove = function(key) {
                if (key in params) params[key] = undefined;
            };
            this.get = function(key) {
                return params[key];
            };
            this.url = function(key) {
                var strurl = href;
                var objps = [];
                for (var k in params) {
                    if (params[k]) {
                        objps.push(k + "=" + params[k]);
                    }
                }
                if (objps.length > 0) {
                    strurl += "?" + objps.join("&");
                }
                if (jing.length > 0) {
                    strurl += jing;
                }
                return strurl;
            };
            this.debug = function() {
                // 以下调试代码自由设置
                var objps = [];
                for (var k in params) {
                    objps.push(k + "=" + params[k]);
                }
                alert(objps); //输出params的所有值
            };
            init();
        }
    }

})
